package com.example.androidproject.file;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidproject.databinding.FragmentFileBinding;
import com.example.androidproject.databinding.FragmentFileItemBinding;
import com.example.androidproject.sqlite.FileRepository;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> {

    private List<FileRepository.FileRecord> list;
    private ItemClickListener listener;

    public FileAdapter(List<FileRepository.FileRecord> list, ItemClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FragmentFileItemBinding binding = FragmentFileItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FileRepository.FileRecord fileRecord = list.get(position);
        holder.bind(fileRecord);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void deleteById(int id) {
        list.remove(id);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private FragmentFileItemBinding binding;

        public ViewHolder(@NonNull FragmentFileItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(FileRepository.FileRecord fileRecord) {
            binding.fileName.setText(fileRecord.getFileName());
            // 将时间戳转换为 LocalDateTime
            LocalDateTime dateTime = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                dateTime = Instant.ofEpochMilli(fileRecord.getTimestamp())
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime();
                // 格式化输出
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedDate = dateTime.format(formatter);
                binding.fileDate.setText(formattedDate);
            }
            if (fileRecord.getSender().equals("0")) {
                binding.senderId.setText("本机");
            }else{
                binding.senderId.setText(fileRecord.getSender());
            }
            binding.linearLayout.setVisibility(View.GONE);
            itemView.setOnClickListener(v -> {
                listener.itemClickEvent(fileRecord);
            });
        }
    }

    interface ItemClickListener {
        void itemClickEvent(FileRepository.FileRecord fileRecord);
    }
}
